<?php
use GatewayClient\Gateway;

class controller_games extends model_games{
    # region 红包游戏管理
    //获取分布及查找参数
    private function games_get_args($get = true){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        return $args;
    }
    //拼装页面参数
    private function games_page_args($args)
    {
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }
    //数据列表
    public function games_list(){
        $basepath = '/games/games_list';

        $get = true;
        $args = $this->games_get_args($get);
        $page_args = $this->games_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_games($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);


        $this->display("games/games_list",array(
            'basepath' => $basepath,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'thishref' => '/games/games_list',
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }
    //获取添加及编辑页面表单参数
    private function games_list_add_edit_args($get = true){
        $para = $get ? $_GET : $_POST;
        unset($para["client_type"]);
        $args = array();
        $args['id'] = $this->existsKeyInArray('id',$para,'N');
        $args['title'] = $this->existsKeyInArray('title',$para,'S');
        $args['func'] = $this->existsKeyInArray('func',$para,'S');
        $args['icon'] = $this->existsKeyInArray('icon',$para,'S');
        $args['establishcost'] = $this->existsKeyInArray('establishcost',$para,'F');
        $args['ishavebanker'] = $this->existsKeyInArray('ishavebanker',$para,'N');
        $args['establishcoin'] = $this->existsKeyInArray('establishcoin',$para,'F');
        $args['manymen'] = $this->existsKeyInArray('manymen',$para,'S');
        $args['betscoinlist'] = $this->existsKeyInArray('betscoinlist',$para,'S');
        $args['roundlist'] = $this->existsKeyInArray('roundlist',$para,'S');
        $args['custombetscoin'] = $this->existsKeyInArray('custombetscoin',$para,'F');
        $args['betscoinlist'] = $this->existsKeyInArray('betscoinlist',$para,'S');
        $args['minbetcoin'] = $this->existsKeyInArray('minbetcoin',$para,'F');
        $args['maxbetcoin'] = $this->existsKeyInArray('maxbetcoin',$para,'F');
        $args['maxodds'] = $this->existsKeyInArray('maxodds',$para,'F');
        $args['overtime'] = $this->existsKeyInArray('overtime',$para,'N');
        $args['state'] = $this->existsKeyInArray('state',$para,'N');
        $args['sort'] = $this->existsKeyInArray('sort',$para,'N');
        $args['content'] = $this->existsKeyInArray('content',$para,'S');
        $args['passcoin'] = $this->existsKeyInArray('passcoin', $para, 'F');
        if(empty($para['title'])) core::error("名称不能为空");
        if(empty($para['func'])) core::error("算法不能为空");
        if(empty($para['overtime'])) core::error("须指定超时时间，单位为秒");
        if(empty($para['manymen'])) core::error("须指定包数");
        if(empty($para['betscoinlist'])) core::error("须设置快捷投注列表");
        if(empty($para['content'])) core::error("游戏规则不能为空");
        return $args;
    }
    //添加
    public function games_list_add(){
        $basepath = '/games/games_list';
        $maxsort = $this->table_maxsort('@@games','sort','1');
        $this->display("games/games_add",array(
            'basepath' => $basepath,
            'maxsort' => $maxsort,
        ));
    }
    //编辑
    public function games_list_edit(){
        $basepath = '/games/games_list';
        $args['id'] = $this->existsKeyInArray('id',$_GET,'N');
        $data = $this->db->getRow('@@games','*',$args);
        $content = $this->db->getRow('@@games_rule','content',array('gameid'=>$args['id']));
        $this->display("games/games_edit",array(
            'basepath' => $basepath,
            'data' => $data,
            'content' => $content,
        ));
    }
    //添加数据保存
    public function games_list_add_save()
    {
        $args = $this->games_list_add_edit_args(false);
        $content = $args['content'];
        unset($args['content']);
        $_result = $this->db->insert('@@games',$args);
        if(!$_result) core::error("添加[".$args['title']."]失败".$_result);
        $this->addLog($this->authid,__FUNCTION__,$_result);
        # 添加规则
        $this->db->insert('@games_rule',array('content'=>$content,'gameid'=>$_result));
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => 'JumpMainFrame("/games/games_list");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));
    }
    //更新数据保存
    public function games_list_edit_save()
    {
        $args = $this->games_list_add_edit_args(false);
        $id = intval($args['id']);
        $content = $args['content'];
        unset($args['content']);
        unset($args['id']);
        $_result = $this->db->update('@@games',$args,array('id'=>$id));
        $_result = $this->db->update('@@games_rule',array('content'=>$content),array('gameid'=>$id));
        //if(!$_result) core::error("编辑[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$_result);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "编辑[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => 'JumpMainFrame("/games/games_list");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));
    }
    //删除指定行
    public function games_list_delete()
    {
        $id = $this->existsKeyInArray('id',$_REQUEST,'N');
        //$sql = "UPDATE `@@users` SET `{$field}`=ABS(`{$field}`-1) WHERE `id`={$id}";
        $sql = "UPDATE `@@games` SET `isdelete`=1 WHERE `id`={$id}";
        $_result = $this->db->query($sql,0);
        if(!$_result) core::error('玩法删除失败，请重试！');
        $this->addLog($this->authid,__FUNCTION__,$_result);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "玩法删除成功",
            'auto' => true,
            'yes'  => array(
                'text' => '我知道了',
                'func' => 'JumpMainFrame("/games/games_list");',
            ),
        ));
    }
    //恢复指定的行
    public function games_list_recovery(){
        $id = $this->existsKeyInArray('id',$_REQUEST,'N');
        //$sql = "UPDATE `@@users` SET `{$field}`=ABS(`{$field}`-1) WHERE `id`={$id}";
        $sql = "UPDATE `@@games` SET `isdelete`=0 WHERE `id`={$id}";
        $data = $this->db->query($sql,0);
        if(!$data) core::error('玩法恢复失败，请重试！');
        $this->addLog($this->authid,__FUNCTION__,$data,'恢复'.$id);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "玩法恢复成功",
            'auto' => true,
            'yes'  => array(
                'text' => '我知道了',
                'func' => 'JumpMainFrame("/games/games_list");',
            ),
        ));
    }
    //删除选定数据行（多行）
    public function games_list_delete_list(){
        if(!$this->post) core::__403();
        $ids = $this->existsKeyInArray('ids',$_POST,'S');
        if(empty($ids)) core::error('没有选择要删除的数据行');
        $_result = $this->db->delete_list('@@games','id',$ids);
        $this->addLog($this->authid,__FUNCTION__,$_result,'删除'.$ids);
        if(!$_result) core::error('删除失败');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "操作成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/games/games_list");',
            ),
        ));
    }
    # endregion

    # region 游戏赔率设置
    public function games_odds_set()
    {
        $gameid = $this->existsKeyInArray('gameid',$_GET,'N');
        $gamename = $this->existsKeyInArray('gamename',$_GET,'S');
        if(empty($gameid)) core::error('参数不正确');
        $basepath = '/games/games_odds_set';
        $this->display("games/games_odds_set",array(
            'basepath' => $basepath,
            'datas' => $this->mod_games_odds($gameid),
            'gameid' => $gameid,
            'gamename' => $gamename,
        ));
    }
    public function games_odds_set_add()
    {
        $args['gameid'] = $this->existsKeyInArray('gameid',$_GET,'N');
        $gamename = $this->existsKeyInArray('gamename',$_GET,'S');

        $html = '<div id="mainbody">';
        $html .= '<div class="main">';
        $html .= '<div class="info">';
        $html .= '<form name="add_save" action="/games/games_odds_set_save"  method="POST" target="ajax" func="form_submit">';
        $html .= '<input type="hidden" name="gameid" value="'.$args['gameid'].'" />';
        $html .= '<dl><dt>游戏：</dt><dd>'.$gamename.'</dd></dl>';
        $html .= '<dl><dt>牌型：</dt><dd><input type="text" name="cardtitle" placeholder="牌型说明" /></dd></dl>';
        $html .= '<dl><dt>牌型编码：</dt><dd><input type="text" name="cardtype" placeholder="计算时用到的编码" /> </dd></dl>';
        $html .= '<dl><dt>对应包数：</dt><dd><input type="text" name="manymen" placeholder="红包所对应的人数" /> </dd></dl>';
        $html .= '<dl><dt>赢输排序：</dt><dd><input type="text" name="winsort" placeholder="牌型越大，数值需越大" /> </dd></dl>';
        $html .= '<dl><dt>赔率：</dt><dd><input type="text" name="odds" placeholder="牌型的赔率" /> 倍</dd></dl>';
        $html .= '<dl><dt>门票：</dt><dd><input type="text" name="ticket" placeholder="赢家、抢到红包门票" /> %</dd></dl>';
        $html .= '<dl><dt>抽水：</dt><dd><input type="text" name="winerprofit" placeholder="赢家、抢到红包抽水" /> %</dd></dl>';
        $html .= '</form>';
        $html .= '</div>';
        $html .= '</div>';
        $html .= '</div>';
        $this->dialogue(array(
            'class' => '',
            'body' => $html,
            'yes'  => array(
                'text' => '确定',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }
    public function games_odds_set_edit()
    {
        $basepath = '/games/games_odds_set';
        $args['id'] = $this->existsKeyInArray('id',$_GET,'N');
        $gamename = $this->existsKeyInArray('gamename',$_GET,'S');

        $data = $this->db->getRow('@@games_odds','*',$args);
        $html = '<div id="mainbody">';
        $html .= '<div class="main">';
        $html .= '<div class="info">';
        $html .= '<form name="add_save" action="/games/games_odds_set_save"  method="POST" target="ajax" func="form_submit">';
        $html .= '<input type="hidden" name="id" value="'.$args['id'].'" /><input type="hidden" name="gameid" value="'.$data['gameid'].'" />';
        $html .= '<dl><dt>游戏：</dt><dd>'.$gamename.'</dd></dl>';
        $html .= '<dl><dt>牌型：</dt><dd><input type="text" name="cardtitle" class="int" placeholder="牌型说明" value="'.$data['cardtitle'].'" /></dd></dl>';
        $html .= '<dl><dt>牌型编码：</dt><dd><input type="text" name="cardtype" class="int" placeholder="计算时用到的编码" value="'.$data['cardtype'].'" /> </dd></dl>';
        $html .= '<dl><dt>对应包数：</dt><dd><input type="text" name="manymen" class="int" placeholder="红包所对应的人数" value="'.$data['manymen'].'" /> </dd></dl>';
        $html .= '<dl><dt>赢输排序：</dt><dd><input type="text" name="winsort" class="int" placeholder="牌型越大，数值需越大" value="'.$data['winsort'].'" /> </dd></dl>';
        $html .= '<dl><dt>赔率：</dt><dd><input type="text" name="odds" class="int" placeholder="牌型的赔率" value="'.$data['odds'].'" /> 倍</dd></dl>';
        $html .= '<dl><dt>门票：</dt><dd><input type="text" name="ticket" class="int" placeholder="赢家、抢到红包门票" value="'.$data['ticket'].'" /> %</dd></dl>';
        $html .= '<dl><dt>抽水：</dt><dd><input type="text" name="winerprofit" class="int" placeholder="赢家、抢到红包抽水" value="'.$data['winerprofit'].'" /> %</dd></dl>';
        $html .= '</form>';
        $html .= '</div>';
        $html .= '</div>';
        $html .= '</div>';
        $this->dialogue(array(
            'class' => '',
            'body' => $html,
            'yes'  => array(
                'text' => '确定',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }
    private function games_odds_set_save_args($get=false)
    {
        $para = $get ? $_GET : $_POST;
        unset($para["client_type"]);
        $args = array();
        $args['id'] = $this->existsKeyInArray('id',$para,'N');
        $args['gameid'] = $this->existsKeyInArray('gameid',$para,'N');
        $args['cardtitle'] = $this->existsKeyInArray('cardtitle',$para,'S');
        $args['manymen'] = $this->existsKeyInArray('manymen',$para,'N');
        $args['cardtype'] = $this->existsKeyInArray('cardtype',$para,'S');
        $args['winsort'] = $this->existsKeyInArray('winsort',$para,'N');
        $args['odds'] = $this->existsKeyInArray('odds',$para,'F');
        $args['winerprofit'] = $this->existsKeyInArray('winerprofit',$para,'F');
        $args['ticket'] = $this->existsKeyInArray('ticket',$para,'F');
        if(empty($para['cardtitle'])) core::error("名称不能为空");
        return $args;

    }
    public function games_odds_set_save()
    {
        $args = $this->games_odds_set_save_args(false);
        $info = "编辑[".$args['cardtitle']."]成功";
        if(empty($args['id']))
        {
            unset($args['id']);
            $_result = $this->db->insert('@@games_odds',$args);
            if(!$_result) core::error("添加[".$args['cardtitle']."]失败".$_result);
            $info = "添加[".$args['title']."]成功";
            $this->addLog($this->authid,__FUNCTION__,$_result);
        }else{
            $id = $args['id'];
            unset($args['id']);
            $_result = $this->db->update('@@games_odds',$args,array('id'=>$id));
            if(!$_result) core::error("编辑[".$args['cardtitle']."]失败");
            $this->addLog($this->authid,__FUNCTION__,$_result,$args['cardtitle']);
        }
        $this->dialogue(array(
            'type' => 'success',
            'text' => $info,
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => '$("#btn_refresh").trigger("click");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));
    }
    # endregion

    # region 游戏发包、抢包、概率设置
    public function games_setup()
    {
        $args['gameid'] = $this->existsKeyInArray('gameid',$_GET,'N');
        $gamename = $this->existsKeyInArray('gamename',$_GET,'S');
        $sql = "SELECT * FROM `@@auto_sb_params` WHERE `gameid`=".$args['gameid']." LIMIT 1";
        $data = $this->db->query($sql,2);
        if($data) {

            $html = '<div id="mainbody">';
            $html .= '<div class="main">';
            $html .= '<div class="info">';
            $html .= '<form name="edit_save" action="/games/games_setup_save"  method="POST" target="ajax" func="form_submit">';
            $html .= '<input type="hidden" name="gameid" value="' . $args['gameid'] . '" />';
            $html .= '<input type="hidden" name="gamename" value="' . $gamename . '" />';
            $html .= '<dl><dt>游戏名称：</dt><dd>' . $gamename . '</dd></dl>';
            $html .= '<dl><dt>机器发包：</dt><dd><select name="autosend">';
            $html .= '<option value="0"'.(intval($data['autosend']) == 0 ? ' selected="selected"' : '').'>关闭</option>';
            $html .= '<option value="1"'.(intval($data['autosend']) == 1 ? ' selected="selected"' : '').'>开启</option>';
            $html .= '</select></dd></dl>';
            $html .= '<dl><dt>机器抢包：</dt><dd><select name="autograb">';
            $html .= '<option value="0"'.(intval($data['autograb']) == 0 ? ' selected="selected"' : '').'>关闭</option>';
            $html .= '<option value="1"'.(intval($data['autograb']) == 1 ? ' selected="selected"' : '').'>开启</option>';
            $html .= '</select></dd></dl>';
            $html .= '<dl><dt>最多发包：</dt><dd><input type="int" name="sendnums" value="'.$data['sendnums'].'" /> </dd></dl>';
            $html .= '<dl><dt>游戏概率：</dt><dd><select name="probability">';
            $html .= '<option value="0"'.(intval($data['probability']) == 0 ? ' selected="selected"' : '').'>关闭</option>';
            $html .= '<option value="1"'.(intval($data['probability']) == 1 ? ' selected="selected"' : '').'>开启</option>';
            $html .= '</select></dd><dd class="t">关闭后所有红包都将随机分配</dd></dl>';
            $html .= '<dl><dt>抢最大包：</dt><dd><select name="grabbig">';
            $html .= '<option value="0"'.(intval($data['grabbig']) == 0 ? ' selected="selected"' : '').'>关闭</option>';
            $html .= '<option value="1"'.(intval($data['grabbig']) == 1 ? ' selected="selected"' : '').'>开启</option>';
            $html .= '</select></dd><dd class="t">概率用户是否有机会拿最大（牌型）包</dd> </dl>';
            $html .= '<dl><dt>抢最小包：</dt><dd><select name="grabmin">';
            $html .= '<option value="0"'.(intval($data['grabmin']) == 0 ? ' selected="selected"' : '').'>关闭</option>';
            $html .= '<option value="1"'.(intval($data['grabmin']) == 1 ? ' selected="selected"' : '').'>开启</option>';
            $html .= '</select></dd><dd class="t">机器人是否有机会拿最小（牌型）包</dd> </dl>';
            $html .= '<dl><dt>发包金额：</dt><dd><input type="text" class="wtxt" name="coinlist" value="'.$data['coinlist'].'" /></dd></dl>';
            $html .= '<dl><dt>&nbsp;</dt><dd class="t">发包的随机金额，用英文 , 分开，机器人不会抢大于最后一个数值的红包</dd></dl>';
            $html .= '<dl><dt>机器创建房间：</dt><dd><select name="autocreate">';
            $html .= '<option value="0"'.(intval($data['autocreate']) == 0 ? ' selected="selected"' : '').'>关闭</option>';
            $html .= '<option value="1"'.(intval($data['autocreate']) == 1 ? ' selected="selected"' : '').'>开启</option>';
            $html .= '</select></dd></dl>';
            $html .= '<dl><dt>最多创建房间数量：</dt><dd><input type="int" name="createroomnums" value="'.$data['createroomnums'].'" /> </dd></dl>';
            $html .= '<dl><dt>循环生成有雷包次数：</dt><dd><input type="int" name="slreplaylen" value="'.$data['slreplaylen'].'" /> </dd></dl>';
            $html .= '</form>';
            $html .= '</div>';
            $html .= '</div>';
            $html .= '</div>';
            $this->dialogue(array(
                'class' => 'mid',
                'body' => $html,
                'yes' => array(
                    'text' => '确定',
                    'func' => '$("form[name=edit_save]").submit();',
                ),
                'no' => array(
                    'text' => '关闭',
                ),
            ));
        }else{
            core::error('没有找到相应的游戏参数');
        }
    }

    public function games_setup_save()
    {
        $gameid = $this->existsKeyInArray('gameid',$_POST,'N');
        $gamename = $this->existsKeyInArray('gamename',$_POST,'S');
        $_update = array(
            'autosend' => $this->existsKeyInArray('autosend', $_POST, 'N'),
            'autograb' => $this->existsKeyInArray('autograb', $_POST, 'N'),
            'sendnums' => $this->existsKeyInArray('sendnums', $_POST, 'N'),
            'probability' => $this->existsKeyInArray('probability', $_POST, 'N'),
            'coinlist' => $this->existsKeyInArray('coinlist', $_POST, 'S'),
            'grabbig' => $this->existsKeyInArray('grabbig', $_POST, 'N'),
            'grabmin' => $this->existsKeyInArray('grabmin', $_POST, 'N'),
            'autocreate' => $this->existsKeyInArray('autocreate', $_POST, 'N'),
            'createroomnums' => $this->existsKeyInArray('createroomnums', $_POST, 'N'),
            'slreplaylen' => $this->existsKeyInArray('slreplaylen', $_POST, 'N'),
        );
        $_result = $this->db->update('@@auto_sb_params',$_update,array('gameid'=>$gameid));
        if(!$_result) core::error("编辑[".$gamename."]失败");
        $this->addLog($this->authid,__FUNCTION__,$_result);
        # 删除缓存
        $this->redis->del($this->redisPrefix['SGPARAMS'].$gameid);
        $this->dialogue(array(
            'type' => 'success',
            'text' => '游戏发包、抢包、概率设置成功',
            'auto' => true,
            'no'  => array(
                'text' => '返回',
            ),
        ));
    }
    # endregion

    # region 自动抢包
    public function autograb(){
        $gameid = $this->existsKeyInArray('gameid',$_GET,'N');
        if(empty($gameid)) core::error('游戏参数不正确');
        # 找出正在进行的红包的类型及用户类型（红包种类）
        $red = $this->mod_autogrob($gameid);
        if(!$red) core::error('红包不存在');
        # 找出除发包玩家的所有余额足够的用户列表，自动登录并开始抢包
        $user = $this->mod_autogrobuser($red);
        if(!$user) core::error('没有适合操作的玩家');
        $args = array(
            'cmd'=>'grab',
            'room_id'=>'game_'.$red['gameid'],
            'game_id'=>intval($red['gameid']),
            'red_issue'=>$red['issue'],
            'pass'=>'',
            'token'=>$user['webtoken'],
        );
        $url = str_replace('https://','http://',$this->config['apiurl']).'/game/grabgameenvelopes?';
        $url .= 'cmd='.$args['cmd'];
        $url .= '&room_id='.$args['room_id'];
        $url .= '&game_id='.$args['game_id'];
        $url .= '&red_issue='.$args['red_issue'];
        $url .= '&pass=';
        $url .= '&token='.$args['token'];
        //初始化
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        //var_dump($output);

        if(strtolower($output)!='null') {
            $data = json_decode($output, true);
            if(count($data)>0 AND $data['state']==200) {
                # 通知玩法房间更新当期红包更新数据人数
                if(isset($data['updatered'])){
                    $data['updatered']['state'] = 200;
                    Gateway::sendToGroup($data['groupname'], json_encode($data['updatered']));
                }
                # 通知当前期数房间有新玩家加入抢包
                if(isset($data['newsjoingrab'])){
                    // Gateway::joinGroup($client_id, $data['reddata']['issue']);
                    Gateway::sendToGroup($data['reddata']['issue'], json_encode($data['newsjoingrab']));
                }
                //组织当前玩家的展示页面数据
                //Gateway::sendToCurrentClient(json_encode($data['curdata']));

                $data['datas'] = $data['reddata'];
                unset($data['reddata']);
                unset($data['updatered']);
                unset($data['newsjoingrab']);
                //unset($data['room_name']);
                $settlement = (isset($data['settlement']) ? $data['settlement'] : false);
                unset($data['settlement']);
                # 通知当前玩家打开抢包页面，列出抢包用户
                // Gateway::sendToCurrentClient(json_encode($data));
                # 通知小组玩家结算
                if($settlement && isset($settlement['issue']))
                {
                    //print_r($settlement);
                    Gateway::sendToGroup($settlement['issue'], json_encode($settlement));
                    # 如果结束当期红包，将要通知参与的用户更新相应的玩家余额
                    if(isset($settlement['userscoin']) && is_array($settlement['userscoin']))
                    {
                        foreach ($settlement['userscoin'] as $k=>$v)
                        {
                            Gateway::sendToUid('P'.$k, json_encode(array(
                                'usercoin'=>$v['coin'],
                                'userscore'=>intval($v['score']),
                                'state'=>200,
                                'cmd'=>'updateusercoin',
                            )));
                        }
                    }
                    # 处理跑马灯内容
                    $marquee = array(
                        'cmd'=>'marquee',
                        'state'=>200
                    );
                    if(isset($settlement['room_name'])) {
                        $gameid = str_replace('game_', '', $settlement['room_name']);
                        $gamename = $this->mod_getGameForID($gameid);
                        $gamename = isset($gamename['data']['title']) ? $gamename['data']['title'] : '';

                        foreach ($settlement['grabusers'] as $k => $v) {
                            if ($v['profit'] > 0) {
                                switch ($v['cardtitle'])
                                {
                                    case '2倍':
                                    case '1.5倍':
                                    case '1倍':
                                    case '比大小':
                                        $v['cardtitle'] = '';
                                        break;
                                    default:
                                        break;
                                }
                                $marquee['datas'][$k] = array(
                                    'title' => $gamename,
                                    'nickname' => $v['nickname'],
                                    'avatar' => $v['avatar'],
                                    'profit' => $v['profit'],
                                    'cardtitle' => $v['cardtitle'],
                                );
                            }
                        }
                        //print_r($settlement);
                        #保存跑马灯的内容
                        $this->redisSetEx($settlement['room_name'], $marquee, 600);
                        $this->redisSetEx('games_marquee_0', $marquee, 600);

                        Gateway::sendToGroup($settlement['room_name'], json_encode($marquee));
                        Gateway::sendToGroup('public', json_encode($marquee));
                    }
                }
                echo json_encode(array(
                    'state'=>200,
                    'msgid'=>200,
                ));
            }
        }else{
            echo json_encode(array(
                'state'=>404,
                'msgid'=>404,
            ));
        }
    }
    # endregion

}